<template>
  <div>

    <!-- 分割线 -->
    <el-divider></el-divider>
    <!-- 新增表单 -->
    <el-form ref="form" :rules="rules" :model="form" label-width="100px" style="width: 300px">
      <el-form-item label="导演姓名" prop="directorName">
        <el-input
          v-model="form.directorName"
          placeholder="请输入姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="选择头像" prop="directorAvatar">
        <el-upload
          class="avatar-uploader"
          :action="`${$UPLOADURL}/upload`"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
        >
          <img v-if="form.directorAvatar" :src="form.directorAvatar" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="onSubmit">新增导演</el-button>
        <el-button type="">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import myaxios from '@/http/MyAxios';
export default {
  data() {
    return {
      form: {
        directorName: "",
        directorAvatar: "",
      },
      // 验证表单
    rules:{
        directorName:[{
            required: true, message: '姓名必须填写', trigger: 'blur'
        }],
        directorAvatar:[{
            required: true, message: '图片必须上传'
        }]
    }
    };
  },
  methods: {
    
    // 点击新增添加导演
    onSubmit() {
        // 验证表单
     this.$refs['form'].validate((valid) => {
            if (valid) {
                 //  valid返回true意味着两个字段验证通过
                    // post请求，执行添加业务
                    let params = this.form
                   this.$httpApi.directorApi.add(params).then(res => {
                       if(res.data.code==200){
                        // 重置表单
                        this.$refs['form'].resetFields()
                        // 成功提示
                        this.$message({
                            message:'新增演员成功',
                            type:'success'
                        })
                       }
                    })
            }
        })
    },

    // 上头像成功后自动调用，将会自动传入响应结果res与文件对象file
    handleAvatarSuccess(res, file) {
      console.log(res);
      this.form.directorAvatar = res.data;
    },
  },
};
</script>

<style lang="scss" >
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>